import axios from 'axios';

function ajax(url=' ', data={}, type='GET') {
    if(type==='GET') {
        let dataStr = ''
        Object.keys(data).forEach(key=> {
            dataStr += key + '=' + data[key] + '&'
        })
        if(dataStr !== '') {
            dataStr = dataStr.substring(0,dataStr.lastIndexOf('&'))
            url = url + '?' + dataStr
        }
        return axios.get(url)
    } else if(type === 'POST'){
        let FD = new FormData();
        for (let name in data) {
            FD.append(name, data[name]);
        }
        return axios.post(url,FD)
    } else if(type === 'PUT'){
        let FD = new FormData();
        for (let name in data) {
            FD.append(name, data[name]);
        }
        return axios.put(url,FD)
    } else if(type==='DELETE') {
        let dataStr = ''
        Object.keys(data).forEach(key=> {
            dataStr += key + '=' + data[key] + '&'
        })
        if(dataStr !== '') {
            dataStr = dataStr.substring(0,dataStr.lastIndexOf('&'))
            url = url + '?' + dataStr
        }
        return axios.delete(url)
    }
}

export const centerContentGET = (user) => ajax('/v1/user/file', user, 'GET');

export const centerDirContent = (user) => ajax('/v1/user/file', user, 'POST');

export const centerDirDelete = (user) => ajax('/v1/user/file', user, 'DELETE');

export const centerContentPut = (user) => ajax('/v1/user/file', user, 'PUT');

export const fileUploadMiddle = (user) => ajax('/v1/user/upload', user, 'GET');

export const fileTryUpload = (user) => ajax('/v1/file/try_upload', user, 'POST');

export const fileUpload = (user) => ajax('/v1/file/upload', user, 'POST');

export const fileDownloadMiddle = (user) => ajax('/v1/user/download', user, 'GET');

export const fileDownload = (user) => ajax('/v1/file/download', user, 'GET');

// export const reqLogin = ({username, password}) => ajax('/login', {username, password},'POST');

// export const reqUpdateUser = (user) => ajax('/update',user,'POST')